<template>
    <div class='search-sort'>
      <div class="sort">
        <a href="javascript:;" :class="{active : sortObj.sortField === ''}" @click="sortFn('')">默认排序</a>
        <a href="javascript:;" :class="{active : sortObj.sortField === 'publishTime'}" @click="sortFn('publishTime')">最新商品</a>
        <a href="javascript:;" :class="{active : sortObj.sortField === 'orderNum'}" @click="sortFn('orderNum')">最高人气</a>
        <a href="javascript:;" :class="{active : sortObj.sortField === 'evaluateNum'}" @click="sortFn('evaluateNum')">评论最多</a>
        <a href="javascript:;"  @click="sortFn('price')">
          价格排序
          <i class="arrow up"   :class="{active : sortObj.sortField === 'price' && sortObj.sortMethod === 'asc'}" />
          <i class="arrow down" :class="{active : sortObj.sortField === 'price' && sortObj.sortMethod === 'desc'}" />
        </a>
      </div>
    </div>
  </template>
<script>
import { reactive } from 'vue'
// 点谁谁亮——思路：
// 1.统一添加点击事件
// 2.点击事件中传入标记名，区分各个a标签
// 3. 声明变量保存当前点击的标记名
// 4.动态class判断变量是否和当前标签的标记相同
import { onBeforeRouteUpdate } from 'vue-router'
export default {
  emits: ['changeSortEV'],
  name: 'SubSort',
  setup (props, { emit }) {
    // const selName = ref('') // 保存当前点击标签的标记名
    // const priceAsc = ref('desc')
    const sortObj = reactive({
      sortField: '',
      sortMethod: 'desc'
    })
    const sortFn = (str) => {
      // 点击后赋值
      sortObj.sortField = str
      // 点击价格排序进入判断内
      if (sortObj.sortField === 'price') {
        // 将价格排序的标记取反
        sortObj.sortMethod === 'desc' ? sortObj.sortMethod = 'asc' : sortObj.sortMethod = 'desc'
      } else {
        // 如果点击非价格排序 那么将价格的标记值回归初始值
        sortObj.sortMethod = 'desc'
      }
      emit('changeSortEV', sortObj)
    }
    // 搜索条件改变 筛选重置
    onBeforeRouteUpdate((to, from, next) => {
      sortObj.sortField = ''
      sortObj.sortMethod = 'desc'
    })
    return {
      sortFn,
      sortObj
    }
  }
}
</script>
  <style scoped lang='less'>
  .search-sort {
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .sort {
      display: flex;
      a {
        height: 30px;
        line-height: 28px;
        border: 1px solid #e4e4e4;
        padding: 0 20px;
        margin-right: 20px;
        color: #999;
        border-radius: 2px;
        position: relative;
        transition: all .3s;
        &.active {
          background: var(--xtx-color);
          border-color: var(--xtx-color);
          color: #fff;
        }
        .arrow {
          position: absolute;
          border: 5px solid transparent;
          right: 8px;
          &.up {
            top: 3px;
            border-bottom-color: #bbb;
              &.active {
              border-bottom-color: var(--xtx-color);
            }
          }
          &.down {
            top: 15px;
            border-top-color: #bbb;
            &.active {
              border-top-color: var(--xtx-color);
            }
          }
        }
      }
    }
  }
  </style>
